package com.youlai.boot.modules.tp.controller;

import com.youlai.boot.modules.tp.dto.response.DashboardAllDataVO;
import com.youlai.boot.modules.tp.dto.query.DateRangeQuery;
import com.youlai.boot.modules.tp.dto.response.*;
import com.youlai.boot.modules.tp.service.TPDashboardService;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.time.LocalDate;

/**
 * TP看板控制器
 * 
 * @author xiheforever
 * @since 2025-06-04
 */
@Tag(name = "TP看板接口")
@RestController
@RequestMapping("/api/tp-dashboard")
@RequiredArgsConstructor
@Validated
public class TPDashboardController {

    
    private final TPDashboardService tpDashboardService;

    @Operation(summary = "获取头部实时信息")
    @GetMapping("/header-info")
    public Result<DashboardHeaderInfoVO> getHeaderInfo() {
        DashboardHeaderInfoVO headerInfo = tpDashboardService.getHeaderInfo();
        return Result.success(headerInfo);
    }

    @Operation(summary = "获取KPI核心指标")
    @GetMapping("/kpi-metrics")
    public Result<KPIMetricsVO> getKPIMetrics(
            @Parameter(description = "开始日期") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        DateRangeQuery dateRange = DateRangeQuery.builder()
                .startDate(startDate)
                .endDate(endDate)
                .build();
        
        KPIMetricsVO metrics = tpDashboardService.getKPIMetrics(dateRange);
        return Result.success(metrics);
    }

    @Operation(summary = "获取详细指标数据")
    @GetMapping("/detail-metrics")
    public Result<DetailMetricsVO> getDetailMetrics(
            @Parameter(description = "开始日期") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        DateRangeQuery dateRange = DateRangeQuery.builder()
                .startDate(startDate)
                .endDate(endDate)
                .build();
        
        DetailMetricsVO metrics = tpDashboardService.getDetailMetrics(dateRange);
        return Result.success(metrics);
    }

    @Operation(summary = "获取销售趋势数据")
    @GetMapping("/sales-trend")
    public Result<SalesTrendResponseVO> getSalesTrend(
            @Parameter(description = "开始日期") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        DateRangeQuery dateRange = DateRangeQuery.builder()
                .startDate(startDate)
                .endDate(endDate)
                .build();
        
        SalesTrendResponseVO salesTrend = tpDashboardService.getSalesTrend(dateRange);
        return Result.success(salesTrend);
    }

    @Operation(summary = "获取品类销售占比数据")
    @GetMapping("/category-sales")
    public Result<CategorySalesResponseVO> getCategorySales(
            @Parameter(description = "开始日期") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        DateRangeQuery dateRange = DateRangeQuery.builder()
                .startDate(startDate)
                .endDate(endDate)
                .build();
        
        CategorySalesResponseVO categorySales = tpDashboardService.getCategorySales(dateRange);
        return Result.success(categorySales);
    }

    @Operation(summary = "获取看板完整数据（一次性获取所有数据）")
    @GetMapping("/all-data")
    public Result<DashboardAllDataVO> getDashboardData(
            @Parameter(description = "开始日期") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        DateRangeQuery dateRange = DateRangeQuery.builder()
                .startDate(startDate)
                .endDate(endDate)
                .build();
        
        DashboardAllDataVO allData = tpDashboardService.getAllData(dateRange);
        return Result.success(allData);
    }

    @Operation(summary = "刷新实时数据")
    @PostMapping("/refresh-realtime")
    public Result<RealTimeDataVO> refreshRealTimeData() {
        RealTimeDataVO realTimeData = tpDashboardService.refreshRealTimeData();
        return Result.success(realTimeData);
    }
} 